***TRANSFERS TO CHILDREN***

foreach y in 96 98 {
use "${original}H`y'D_TC.dta", clear
g year = 19`y'
rename *,lower
save h19`y'tc.dta, replace
}

use "${original}H00D_TC.dta", clear
g year = 2000
rename *,lower
save h2000tc.dta, replace

foreach y in 02 04 06 08 10 12 14 {
use "${original}H`y'E_TC.dta", clear
g year = 20`y'
rename *,lower
save h20`y'tc.dta, replace
}

***

use h1996tc.dta, clear
ren *typtran typtran
ren ?pn_cs pn
drop if typtran=="2" // transfers to grandchildren
ren (e1444 e1449) (chtr_numch chtr_amount1996)
drop if chtr_amount==999998 | chtr_amount==999999 | chtr_amount==999997 | chtr_amount==. // DK | refused | other | missing
collapse (sum) chtr_amount , by(hhid pn esubhh)
g year = 1996
save h1996tc_coll.dta, replace

use h1998tc.dta, clear
ren ?pn_cs pn
ren (f1864 f1868) (chtr_numch chtr_amount1998)
drop if chtr_amount==9999998 | chtr_amount==9999999 | chtr_amount==9999997 | chtr_amount==. // DK | refused | other | missing
collapse (sum) chtr_amount , by(hhid pn fsubhh)
g year = 1998
save h1998tc_coll.dta, replace

use h2000tc.dta, clear
ren ?pn_cs pn
ren (g2080 g2084) (chtr_numch chtr_amount2000)
drop if chtr_amount==9999998 | chtr_amount==9999999 | chtr_amount==9999997 | chtr_amount==. // DK | refused | other | missing
collapse (sum) chtr_amount , by(hhid pn gsubhh)
g year = 2000
save h2000tc_coll.dta, replace

use h2002tc.dta, clear
ren ?pn_cs pn
ren (htc_ndx he081) (chtr_numch chtr_amount2002)
drop if chtr_amount==9999998 | chtr_amount==9999999 | chtr_amount==9999997 | chtr_amount==. // DK | refused | other | missing
collapse (sum) chtr_amount , by(hhid pn hsubhh)
g year = 2002
save h2002tc_coll.dta, replace

use h2004tc.dta, clear
ren ?pn_cs pn
ren (jtc_ndx je081) (chtr_numch chtr_amount2004)
drop if chtr_amount==999998 | chtr_amount==999999 | chtr_amount==999997 | chtr_amount==. // DK | refused | other | missing
destring je076m1, gen(opn)
collapse (sum) chtr_amount , by(hhid pn jsubhh)
g year = 2004
save h2004tc_coll.dta, replace

use h2006tc.dta, clear
egen ktc_ndx = rownonmiss(ke076 ke079m1 ke079m2 ke079m3 ke079m4),strok
ren ?pn_cs pn
ren (ktc_ndx ke081) (chtr_numch chtr_amount2006)
drop if chtr_amount==9999998 | chtr_amount==9999999 | chtr_amount==9999997 | chtr_amount==. // DK | refused | other | missing
collapse (sum) chtr_amount , by(hhid pn ksubhh)
g year = 2006
save h2006tc_coll.dta, replace

use h2008tc.dta, clear
egen ltc_ndx = rownonmiss(le076 le079m1 le079m2 le079m3 le079m4),strok
ren ?pn_cs pn
ren (ltc_ndx le081) (chtr_numch chtr_amount2008)
drop if chtr_amount==9999998 | chtr_amount==9999999 | chtr_amount==9999997 | chtr_amount==. // DK | refused | other | missing
collapse (sum) chtr_amount , by(hhid pn lsubhh)
g year = 2008
save h2008tc_coll.dta, replace

use h2010tc.dta, clear
egen mtc_ndx = rownonmiss(me076 me079m1 me079m2 me079m3 me079m4),strok
ren ?pn_cs pn
ren (mtc_ndx me081) (chtr_numch chtr_amount2010)
drop if chtr_amount==9999998 | chtr_amount==9999999 | chtr_amount==9999997 | chtr_amount==. // DK | refused | other | missing
collapse (sum) chtr_amount , by(hhid pn msubhh)
g year = 2010
save h2010tc_coll.dta, replace

use h2012tc.dta, clear
egen ntc_ndx = rownonmiss(ne076 ne079m1 ne079m2 ne079m3 ne079m4),strok
ren ?pn_cs pn
ren (ntc_ndx ne081) (chtr_numch chtr_amount2012)
drop if chtr_amount==9999998 | chtr_amount==9999999 | chtr_amount==9999997 | chtr_amount==. // DK | refused | other | missing
collapse (sum) chtr_amount , by(hhid pn nsubhh)
g year = 2012
save h2012tc_coll.dta, replace

use h2014tc.dta, clear
egen otc_ndx = rownonmiss(oe076 oe079m1 oe079m2 oe079m3 oe079m4),strok
ren ?pn_cs pn
ren (otc_ndx oe081) (chtr_numch chtr_amount2014)
drop if chtr_amount==9999998 | chtr_amount==9999999 | chtr_amount==9999997 | chtr_amount==. // DK | refused | other | missing
collapse (sum) chtr_amount , by(hhid pn osubhh)
g year = 2014
save h2014tc_coll.dta, replace

order hhid *subhh
sort hhid *subhh

foreach y in 1996 1998 2000 2002 2004 2006 2008 2010 2012 2014 {
use h`y'tc_coll.dta, clear
ren *`y' *
save h`y'app, replace
}

use h1996app.dta, clear
foreach y in 1998 2000 2002 2004 2006 2008 2010 2012 2014 {
append using h`y'app.dta
}

ren (esubhh fsubhh gsubhh hsubhh jsubhh ksubhh lsubhh msubhh nsubhh osubhh) (subhh1996 subhh1998 subhh2000 subhh2002 subhh2004 subhh2006 subhh2008 subhh2010 subhh2012 subhh2014)

g str _subhh=""
foreach y in 1996 1998 2000 2002 2004 2006 2008 2010 2012 2014 {
replace _subhh = subhh`y' if year == `y'
}
drop subhh*
ren _subhh subhh

destring hhid pn subhh,replace

lab var chtr_amount "Amount transferred to child(ren)"

save h_core_app.dta, replace

*Merge
use sample_wgt,clear
destring hhid pn subhh,replace
merge 1:1 hhid pn subhh year using h_core_app.dta, gen(chtr) keep(1 3)
lab var chtr "_merge child transfer file"
save sample_chtr,replace

*Erase temporary datasets
foreach n in 1996 1998 2000 2002 2004 2006 2008 2010 2012 2014 {
erase h`n'tc.dta
erase h`n'tc_coll.dta
erase h`n'app.dta
}
erase h_core_app.dta
erase sample_wgt.dta